﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TagEditorViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the tagEditorViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.ViewModel
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Windows;
    using Business.Interfaces;
    using Hisaab.Common;
    using Hisaab.Windows.Common;

    using Microsoft.Practices.ServiceLocation;

    using Model;

    /// <summary>
    /// Defines the <seealso cref="TagEditorViewModel"/> type.
    /// </summary>
    public class TagEditorViewModel : DialogViewModel
    {
        /// <summary>
        /// Tag manager
        /// </summary>
        private readonly ITagManager _tagManager;

        /// <summary>
        /// The session manager
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// Selected tag
        /// </summary>
        private Tag _selectedTag;

        /// <summary>
        /// Initializes a new instance of the <see cref="TagEditorViewModel"/> class.
        /// </summary>
        /// <param name="tag">The tag to add/edit</param>
        public TagEditorViewModel(Tag tag)
            : this(tag, ServiceLocator.Current.GetInstance<ITagManager>(), ServiceLocator.Current.GetInstance<ISession>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TagEditorViewModel"/> class.
        /// </summary>
        /// <param name="tag">The tag to edit</param>
        /// <param name="tagManager">The tag manager.</param>
        /// <param name="session">The session</param>
        public TagEditorViewModel(Tag tag, ITagManager tagManager, ISession session)
        {
            Contract.Requires<ArgumentNullException>(tagManager != null);
            Contract.Requires<ArgumentNullException>(session != null);

            _tagManager = tagManager;
            _session = session;
            _selectedTag = tag ?? new Tag();
            IsNewTag = tag == null;
            ResizeMode = ResizeMode.NoResize;
            WindowStyle = WindowStyle.SingleBorderWindow;
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is new tag.
        /// </summary>
        /// <value>
        /// True if this instance is new tag; otherwise, false.
        /// </value>
        public bool IsNewTag { get; set; }

        /// <summary>
        /// Gets the dialog type string.
        /// </summary>
        /// <value>
        /// The dialog type string.
        /// </value>
        public string DialogTypeString
        {
            get
            {
                return IsNewTag ? "Add" : "Edit";
            }
        }

        /// <summary>
        /// Gets or sets the selected tag.
        /// </summary>
        /// <value>
        /// The selected tag.
        /// </value>
        public Tag SelectedTag
        {
            get
            {
                return _selectedTag;
            }

            set
            {
                if (_selectedTag != value)
                {
                    _selectedTag = value;
                    OnPropertyChanged("SelectedTag");
                }
            }
        }

        /// <summary>
        /// Called when [ok pressed].
        /// </summary>
        /// <returns>
        /// True if dialog to be closed, false to avoid closing
        /// </returns>
        public override bool? OnOkDialog()
        {
            if (IsNewTag)
            {
                SelectedTag.UserId = _session.LoggedUser;
                _tagManager.Add(SelectedTag.Map<Tag, Business.Entities.Tag>());
            }
            else
            {
                _tagManager.Update(SelectedTag.Map<Tag, Business.Entities.Tag>());
            }

            return true;
        }
    }
}